# PoP - Policing Socio-Geographic Boundaries and Inequality
# script for making maps of race and SES boundaries by city


suppressPackageStartupMessages(
  
  {
    library(dplyr)
    library(tidyverse)
    library(ggplot2)
    library(sensemakr)
    library(haven)
    library(readxl)
    library(readr)
    library(areal)
    library(car)
    library(estimatr)
    library(magrittr)
    library(texreg)
    library(sandwich)
    library(jtools)
    library(ggthemes)
    library(meta)
    library(gridExtra)
  }
)

#### Atlanta ####
load("atl_final.RData")

# map % black atlanta
ATL_p_black_map = atl_fin['p_race_black'] %>% ggplot() + geom_sf(aes(fill = p_race_black), size = 0) + 
  labs(fill = "% Black") + ggthemes::theme_tufte()

ggsave(plot = ATL_p_black_map, filename = "ATL_p_black_map.png", 
       height = 8, width = 12, bg = "white")

ATL_p_white_map = atl_fin['p_race_white'] %>% ggplot() + geom_sf(aes(fill = p_race_white), size = 0) + 
  labs(fill = "% White") + ggthemes::theme_tufte()

ggsave(plot = ATL_p_white_map, filename = "ATL_p_white_map.png", 
       height = 8, width = 12, bg = "white")

ATL_race_bound_map = atl_fin['p_race_white_blv'] %>% 
  ggplot() + geom_sf(aes(fill = p_race_white_blv), size = 0) +
  labs(fill = "Boundary") + ggthemes::theme_tufte()

ggsave(plot = ATL_race_bound_map, filename = "ATL_race_bound_map.png", 
       height = 8, width = 12, bg = "white")

# put ATL maps side by side
library(gridExtra)
atl_maps = arrangeGrob(ATL_p_white_map, ATL_race_bound_map, ncol = 2)

ggsave(plot = atl_maps, filename = "atl_maps.png", 
       height = 10, width = 12, bg = "white")

#### Austin ####

load("aus_final.RData")

# map % black ausanta
AUS_p_nonwhite_map = aus_fin['p_race_nonwhite'] %>% ggplot() + geom_sf(aes(fill = p_race_nonwhite), size = 0) + 
  labs(fill = "% Non-white") + ggthemes::theme_tufte()

ggsave(plot = AUS_p_nonwhite_map, filename = "AUS_p_nonwhite_map.png", 
       height = 8, width = 12, bg = "white")

AUS_p_white_map = aus_fin['p_race_white'] %>% ggplot() + geom_sf(aes(fill = p_race_white), size = 0) + 
  labs(fill = "% White") + ggthemes::theme_tufte()

ggsave(plot = AUS_p_white_map, filename = "AUS_p_white_map.png", 
       height = 8, width = 12, bg = "white")

AUS_race_bound_map = aus_fin['p_race_white_blv'] %>% 
  ggplot() + geom_sf(aes(fill = p_race_white_blv), size = 0) +
  labs(fill = "Boundary") + ggthemes::theme_tufte()

ggsave(plot = AUS_race_bound_map, filename = "AUS_race_bound_map.png", 
       height = 8, width = 12, bg = "white")

#### Boston ####
load("bos_final.RData")

# map % black boston
BOS_p_nonwhite_map = bos_fin['p_race_nonwhite'] %>% ggplot() + geom_sf(aes(fill = p_race_nonwhite), size = 0) + 
  labs(fill = "% Non-white") + ggthemes::theme_tufte()

ggsave(plot = BOS_p_nonwhite_map, filename = "BOS_p_nonwhite_map.png", 
       height = 8, width = 12, bg = "white")

BOS_p_white_map = bos_fin['p_race_white'] %>% ggplot() + geom_sf(aes(fill = p_race_white), size = 0) + 
  labs(fill = "% White") + ggthemes::theme_tufte()

ggsave(plot = BOS_p_white_map, filename = "BOS_p_white_map.png", 
       height = 8, width = 12, bg = "white")

BOS_race_bound_map = bos_fin['p_race_white_blv'] %>% 
  ggplot() + geom_sf(aes(fill = p_race_white_blv), size = 0) +
  labs(fill = "Boundary") + ggthemes::theme_tufte()

ggsave(plot = BOS_race_bound_map, filename = "BOS_race_bound_map.png", 
       height = 8, width = 12, bg = "white")


#### Chicago ####
load("chi_final.RData")

# map % nonwhite chicago
CHI_p_nonwhite_map = chi_fin['p_race_nonwhite'] %>% ggplot() + geom_sf(aes(fill = p_race_nonwhite), size = 0) + 
  labs(fill = "% Non-white") + ggthemes::theme_tufte()

sum(is.na(chi_fin$p_race_nonwhite))
summary(chi_fin$p_race_nonwhite)

ggsave(plot = CHI_p_nonwhite_map, filename = "CHI_p_nonwhite_map.png", 
       height = 8, width = 12, bg = "white")

CHI_p_white_map = chi_fin['p_race_white'] %>% ggplot() + geom_sf(aes(fill = p_race_white), size = 0) + 
  labs(fill = "% White") + ggthemes::theme_tufte()

ggsave(plot = CHI_p_white_map, filename = "CHI_p_white_map.png", 
       height = 8, width = 12, bg = "white")

CHI_race_bound_map = chi_fin['p_race_white_blv'] %>% 
  ggplot() + geom_sf(aes(fill = p_race_white_blv), size = 0) +
  labs(fill = "Boundary") + ggthemes::theme_tufte()

ggsave(plot = CHI_race_bound_map, filename = "CHI_race_bound_map.png", 
       height = 8, width = 12, bg = "white")


#### Louisville ####
load("lou_final.RData")

# map % nonwhite chicago
LOU_p_nonwhite_map = lou_fin['p_race_nonwhite'] %>% ggplot() + geom_sf(aes(fill = p_race_nonwhite), size = 0) + 
  labs(fill = "% Non-white") + ggthemes::theme_tufte()

sum(is.na(lou_fin$p_race_nonwhite))
summary(lou_fin$p_race_nonwhite)

ggsave(plot = LOU_p_nonwhite_map, filename = "LOU_p_nonwhite_map.png", 
       height = 8, width = 12, bg = "white")

LOU_p_white_map = lou_fin['p_race_white'] %>% ggplot() + geom_sf(aes(fill = p_race_white), size = 0) + 
  labs(fill = "% White") + ggthemes::theme_tufte()

ggsave(plot = LOU_p_white_map, filename = "LOU_p_white_map.png", 
       height = 8, width = 12, bg = "white")

LOU_race_bound_map = lou_fin['p_race_white_blv'] %>% 
  ggplot() + geom_sf(aes(fill = p_race_white_blv), size = 0) +
  labs(fill = "Boundary") + ggthemes::theme_tufte()

ggsave(plot = LOU_race_bound_map, filename = "LOU_race_bound_map.png", 
       height = 8, width = 12, bg = "white")

#### Milwaukee ####

load("mil_final.RData")

# map % nonwhite milwaukee
MIL_p_nonwhite_map = mil_fin['p_race_nonwhite'] %>% ggplot() + geom_sf(aes(fill = p_race_nonwhite), size = 0) + 
  labs(fill = "% Non-white") + ggthemes::theme_tufte()

sum(is.na(mil_fin$p_race_nonwhite))
summary(mil_fin$p_race_nonwhite)

ggsave(plot = MIL_p_nonwhite_map, filename = "MIL_p_nonwhite_map.png", 
       height = 8, width = 12, bg = "white")

MIL_p_white_map = mil_fin['p_race_white'] %>% ggplot() + geom_sf(aes(fill = p_race_white), size = 0) + 
  labs(fill = "% White") + ggthemes::theme_tufte()

ggsave(plot = MIL_p_white_map, filename = "MIL_p_white_map.png", 
       height = 8, width = 12, bg = "white")

MIL_race_bound_map = mil_fin['p_race_white_blv'] %>% 
  ggplot() + geom_sf(aes(fill = p_race_white_blv), size = 0) +
  labs(fill = "Boundary") + ggthemes::theme_tufte()

ggsave(plot = MIL_race_bound_map, filename = "MIL_race_bound_map.png", 
       height = 8, width = 12, bg = "white")

# put ATL maps side by side
library(gridExtra)
mil_maps = arrangeGrob(MIL_p_white_map, MIL_race_bound_map, ncol = 2)

ggsave(plot = mil_maps, filename = "mil_maps.png", 
       height = 10, width = 12, bg = "white")


#### Seattle ####
load("sea_final.RData")

# map % nonwhite milwaukee
SEA_p_nonwhite_map = sea_fin['p_race_nonwhite'] %>% ggplot() + geom_sf(aes(fill = p_race_nonwhite), size = 0) + 
  labs(fill = "% Non-white") + ggthemes::theme_tufte()

sum(is.na(sea_fin$p_race_nonwhite))
summary(sea_fin$p_race_nonwhite)

ggsave(plot = SEA_p_nonwhite_map, filename = "SEA_p_nonwhite_map.png", 
       height = 8, width = 12, bg = "white")

SEA_p_white_map = sea_fin['p_race_white'] %>% ggplot() + geom_sf(aes(fill = p_race_white), size = 0) + 
  labs(fill = "% White") + ggthemes::theme_tufte()

ggsave(plot = SEA_p_white_map, filename = "SEA_p_white_map.png", 
       height = 8, width = 12, bg = "white")

SEA_race_bound_map = sea_fin['p_race_white_blv'] %>% 
  ggplot() + geom_sf(aes(fill = p_race_white_blv), size = 0) +
  labs(fill = "Boundary") + ggthemes::theme_tufte()

ggsave(plot = SEA_race_bound_map, filename = "SEA_race_bound_map.png", 
       height = 8, width = 12, bg = "white")

# put all maps side by side
all_city_maps = arrangeGrob(ATL_p_white_map, ATL_race_bound_map, 
                            AUS_p_white_map, AUS_race_bound_map,
                            BOS_p_white_map, BOS_race_bound_map, 
                            CHI_p_white_map, CHI_race_bound_map, 
                            LOU_p_white_map, LOU_race_bound_map,
                            MIL_p_white_map, MIL_race_bound_map,
                            SEA_p_white_map, SEA_race_bound_map,
                            ncol = 2, nrow = 7)

ggsave(plot = all_city_maps, filename = "all_city_maps.png", 
       height = 8, width = 15, bg = "white")

#### Atlanta ####
load("atl_final.RData")

ATL_p_white_map = atl_fin['p_race_white'] %>% ggplot() + geom_sf(aes(fill = p_race_white), size = 0) + 
  labs(fill = "% White") + ggthemes::theme_tufte()

ATL_race_bound_map = atl_fin['p_race_white_blv'] %>% 
  ggplot() + geom_sf(aes(fill = p_race_white_blv), size = 0) +
  labs(fill = "Boundary") + ggthemes::theme_tufte()

#### Austin ####

load("aus_final.RData")

AUS_p_white_map = aus_fin['p_race_white'] %>% ggplot() + geom_sf(aes(fill = p_race_white), size = 0) + 
  labs(fill = "% White") + ggthemes::theme_tufte()

AUS_race_bound_map = aus_fin['p_race_white_blv'] %>% 
  ggplot() + geom_sf(aes(fill = p_race_white_blv), size = 0) +
  labs(fill = "Boundary") + ggthemes::theme_tufte()

#### Boston ####
load("bos_final.RData")

BOS_p_white_map = bos_fin['p_race_white'] %>% ggplot() + geom_sf(aes(fill = p_race_white), size = 0) + 
  labs(fill = "% White") + ggthemes::theme_tufte()

BOS_race_bound_map = bos_fin['p_race_white_blv'] %>% 
  ggplot() + geom_sf(aes(fill = p_race_white_blv), size = 0) +
  labs(fill = "Boundary") + ggthemes::theme_tufte()


#### Chicago ####

load("chi_final.RData")

CHI_p_white_map = chi_fin['p_race_white'] %>% ggplot() + geom_sf(aes(fill = p_race_white), size = 0) + 
  labs(fill = "% White") + ggthemes::theme_tufte()

CHI_race_bound_map = chi_fin['p_race_white_blv'] %>% 
  ggplot() + geom_sf(aes(fill = p_race_white_blv), size = 0) +
  labs(fill = "Boundary") + ggthemes::theme_tufte()

#### Louisville ####
load("final_data/lou_final.RData")

LOU_p_white_map = lou_fin['p_race_white'] %>% ggplot() + geom_sf(aes(fill = p_race_white), size = 0) + 
  labs(fill = "% White") + ggthemes::theme_tufte()


LOU_race_bound_map = lou_fin['p_race_white_blv'] %>% 
  ggplot() + geom_sf(aes(fill = p_race_white_blv), size = 0) +
  labs(fill = "Boundary") + ggthemes::theme_tufte()

#### Milwaukee ####

load("mil_final.RData")

MIL_p_white_map = mil_fin['p_race_white'] %>% ggplot() + geom_sf(aes(fill = p_race_white), size = 0) + 
  labs(fill = "% White") + ggthemes::theme_tufte()

MIL_race_bound_map = mil_fin['p_race_white_blv'] %>% 
  ggplot() + geom_sf(aes(fill = p_race_white_blv), size = 0) +
  labs(fill = "Boundary") + ggthemes::theme_tufte()


#### Seattle ####
load("sea_final.RData")

SEA_p_white_map = sea_fin['p_race_white'] %>% ggplot() + geom_sf(aes(fill = p_race_white), size = 0) + 
  labs(fill = "% White") + ggthemes::theme_tufte()


SEA_race_bound_map = sea_fin['p_race_white_blv'] %>% 
  ggplot() + geom_sf(aes(fill = p_race_white_blv), size = 0) +
  labs(fill = "Boundary") + ggthemes::theme_tufte()



# Arrange all maps side by side in a 7x2 grid with better spacing
all_city_maps = arrangeGrob(ATL_p_white_map, ATL_race_bound_map, 
                            AUS_p_white_map, AUS_race_bound_map,
                            BOS_p_white_map, BOS_race_bound_map, 
                            CHI_p_white_map, CHI_race_bound_map, 
                            LOU_p_white_map, LOU_race_bound_map,
                            MIL_p_white_map, MIL_race_bound_map,
                            SEA_p_white_map, SEA_race_bound_map,
                            ncol = 2, nrow = 7,
                            # Set widths and heights to reduce space between maps
                            heights = unit(rep(1, 7), "null"),  # Equal height for rows
                            widths = unit(rep(1, 2), "null")    # Equal width for columns
)

ggsave(plot = all_city_maps, filename = "all_city_maps.png", 
       height = 14, width = 20, bg = "white")

